package com.odontosis.rel;

import java.io.ByteArrayOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.PdfWriter;
import com.odontosis.util.Constantes;

public class DocumentoBasico {
	
	private Document document = new Document();
	
	private ByteArrayOutputStream baos = new ByteArrayOutputStream();
	
	private HttpServletResponse response;
	
	private final int FORMATO;
	
	
	public DocumentoBasico(int formato, HttpServletResponse response) throws Exception{
		this.response = response;
		this.FORMATO = formato;
		if(formato == Constantes.RTF){
			PdfWriter.getInstance(document, baos);
			document.open();
		}else if(formato == Constantes.PDF){
			PdfWriter.getInstance(document, baos);
			document.open();
		}
	}
	
	public void adicionarElemento(Element elemento) throws Exception{
		document.add(elemento);
	}
	
	public void fecharDocumento() throws Exception{
		document.close();
		if(FORMATO == Constantes.RTF){
			response.setContentType("application/rtf");
		}else if(FORMATO == Constantes.PDF){
			response.setContentType("application/pdf");
		}
		response.setContentLength(baos.size());
		ServletOutputStream out = response.getOutputStream();
		baos.writeTo(out);
		out.flush();
	}
	
	
	/*********************Getters and Setters*****************************************************/
	
	
	
	public ByteArrayOutputStream getBaos() {
		return baos;
	}

	public void setBaos(ByteArrayOutputStream baos) {
		this.baos = baos;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	
}
